package customer.zspt.cn.customer.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**中文转拼音
 * Created by Administrator on 2018/6/8 0008.
 */


    public class Chinese2SpellUtils {

        public static String getPingYin(String inputString,int type) {
            HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
            format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            format.setVCharType(HanyuPinyinVCharType.WITH_V);
            char[] input = inputString.trim().toCharArray();
            String output = "";
            try {
                for (char curchar : input) {
                    if (java.lang.Character.toString(curchar).matches("[\\u4E00-\\u9FA5]+")) {
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(curchar, format);
                        if(type==0)
                        {
                            //返回首字母
                            output += temp[0].charAt(0);
                        }
                        else if(type==1)
                        {
                            //返回全拼
                            output += temp[0];
                        }
                        else
                        {
                            //返回首字母
                            output += temp[0].charAt(0);
                            break;
                        }

                    } else {
                        output += java.lang.Character.toString(curchar);
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
            return output;
        }
    }

